package com.tiger.cloud.controller.crm;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tiger.cloud.common.mybatis.BaseController;
import com.tiger.cloud.enums.DeleteEnum;
import com.tiger.cloud.filter.CurrentUserId;
import com.tiger.cloud.model.crm.LoanRecord;
import com.tiger.cloud.service.crm.ILoanRecordService;
import com.tiger.cloud.util.Page;
import com.tiger.cloud.util.Response;
import com.tiger.cloud.util.ResponseUtil;

import io.swagger.annotations.ApiOperation;

/**
 * 合作记录/借款记录
 */
@Controller
@RequestMapping(value = "loanRecord")
public class LoanRecordController extends BaseController {

	private static final Logger logger=Logger.getLogger(LoanRecordController.class);
	
	@Autowired
	private ILoanRecordService loanRecordService;
	
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "分页查找合作/借款记录",notes = "合作记录/借款记录")
	public Response find(@CurrentUserId Long userId,@RequestBody LoanRecord loanRecord){
		loanRecord.setUserId(userId);
		Page<LoanRecord> datas=loanRecordService.page(loanRecord);
		return ResponseUtil.ok(datas);
	}
	
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "列表查找合作/借款记录",notes = "合作记录/借款记录")
	public Response list(@CurrentUserId Long userId,@RequestBody LoanRecord loanRecord){
		loanRecord.setUserId(userId);
		return ResponseUtil.ok(loanRecordService.find(loanRecord));
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "根新合作/借款记录",notes = "合作记录/借款记录")
	public Response update(@CurrentUserId Long userId,@RequestBody LoanRecord loanRecord){
		if(loanRecord.getId()!=null&&loanRecord.getId()>0){
			LoanRecord updateEntity=loanRecordService.get(loanRecord.getId());
			if(updateEntity==null||updateEntity.getUserId()!=userId){
				return ResponseUtil.error("参数错误,请重试!");
			}
			loanRecord.setUserId(userId);
			if(loanRecordService.update(loanRecord)>0){
				return ResponseUtil.ok();
			};
			return ResponseUtil.error();
		}
		return ResponseUtil.error("参数错误,请重试!");
	}
	
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "添加合作/借款记录",notes = "合作记录/借款记录")
	public Response add(@CurrentUserId Long userId,@RequestBody LoanRecord loanRecord){
		loanRecord.setUserId(userId);
		loanRecordService.insert(loanRecord);
		return ResponseUtil.ok();
	}

	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "删除合作/借款记录",notes = "合作记录/借款记录")
	public Response delete(@CurrentUserId Long userId,@RequestBody LoanRecord loanRecord){
		if(loanRecord.getId()!=null&&loanRecord.getId()>0){
			LoanRecord deleteEntity=loanRecordService.get(loanRecord.getId());
			if(deleteEntity==null||deleteEntity.getUserId()!=userId){
				return ResponseUtil.error("参数错误,请重试!");
			}
			loanRecord.setUserId(userId);
			loanRecord.setIsDelete(DeleteEnum.DELETE);
			if(loanRecordService.update(loanRecord)>0){
				return ResponseUtil.ok();
			};
			return ResponseUtil.error();
		}
		return ResponseUtil.error("参数错误,请重试!");
	}

}